# -*- coding:utf-8 -*-

from __future__ import division

import os
import configparser
from conf.innerpyapollo.apollo_client import ApolloClient
import jsonpickle


class EnvConfig(object):
    def __init__(self):

        # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
        config_file = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/innerpyapollo/backend.ini"
        config = configparser.ConfigParser()

        config.read(config_file)
        client = ApolloClient(app_id=config.get('apollo_server', 'app_id'),
                              config_server_url=config.get('apollo_server', 'config_server_url'),
                              ip=config.get('apollo_server', 'ip'))
        client.start(use_eventlet=True)
        self.result_all = client.all()

        # Common Section
        mysql_result = jsonpickle.loads(self.result_all['backend_mysql_info'])
        # mysql info
        self.backend_mysql_db_name = mysql_result.get('backend_db_name')
        self.backend_mysql_db_user = mysql_result.get('backend_db_user')
        self.backend_mysql_db_password = mysql_result.get('backend_db_password')
        self.backend_mysql_db_host = mysql_result.get('backend_db_host')
        self.backend_mysql_db_port = mysql_result.get('backend_db_port')

